//
// Created by wangjiang on 2016/3/10.
//

#include <stdlib.h>
#include <android/log.h>
#include<stdio.h>
#include "library.h"
#include <fcntl.h>
#include <sys/inotify.h>

#define info(tag, msg) __android_log_write(ANDROID_LOG_DEBUG, tag, msg)
/**
 * Dynamic JNI register, please refer to following url:
 * http://blog.csdn.net/bigapple88/article/details/6756204
 */

static JNINativeMethod METHODS[]= {
        {"test", "(Ljava/lang/String;)V", (void *)test}
};


void test(JNIEnv *env, jclass thiz, jstring path) {
    pid_t pid = fork();
    char file_path[200];
    strcpy(file_path, (*env)->GetStringUTFChars(env, path, 0));
    info("so", file_path);

    char *dy = malloc(24);
    sprintf(file_path, "allocated addr: %x", &dy);
    info("so", file_path);
}

#define NAME_MAX 255
#define BUF_SIZE (sizeof(struct inotify_event) + NAME_MAX + 1)


JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
    int ret = -1;
    JNIEnv *env;
    if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_4) == JNI_OK) {
        jclass clazz = (*env)->FindClass(env,"com/jc/so/SoLoad");
        if (clazz != NULL) {
            if ((*env)->RegisterNatives(env, clazz, METHODS, sizeof(METHODS) / sizeof(METHODS[0])) >= 0) {
                ret = JNI_VERSION_1_4;
            }
            (*env)->DeleteLocalRef(env,clazz);
        }
    }
    return ret;
}

JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved) {}